package cn.yunhe.dao.impl;

import cn.yunhe.dao.INewsDao;
import cn.yunhe.entity.News;
import cn.yunhe.util.PageUtil;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("newsDao")
public class NewsDaoImpl extends HibernateDaoSupport implements INewsDao {

    @Override
    public List<News> getAllNews(int pageIndex) {
        try {
            Criteria criteria = getSession().createCriteria(News.class);
            List<News> newsList = criteria.setFirstResult((pageIndex-1)*PageUtil.PAGE_SIZE).setMaxResults(PageUtil.PAGE_SIZE).list();
            return newsList;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            releaseSession(getSession());
        }

    }

    @Override
    public int getTotalRows() {
        try {
            Criteria criteria = getSession().createCriteria(News.class);
            List<News> newsList = criteria.list();
            return newsList.size();
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }finally {
            releaseSession(getSession());
        }

    }

}
